﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web.Management;

namespace dao.daos
{
    public class ClubDao : BaseDao
    {
        public ClubDao() : base("clubes", "id_club", true)
        {

        }

        public DataTable ObtenerPorNombre(string nombre)
        {
            Conectar();
            DataTable dt = new DataTable();
            try
            {
                Cmd.CommandText = "SELECT * from clubes where razon_social LIKE '%" + nombre + "%'";
                Cmd.Parameters.Add(new SqlParameter("@razon_social", nombre));
                dt.Load(Cmd.ExecuteReader());
                return dt;
                
            }
            catch (Exception e)
            {
                throw new SqlExecutionException("Error al obtener club por razón social", e);
            }
            Conexion.Close();
        }

        public bool Existe(string nombre)
        {
            SqlDataReader dr = null;
            try
            {
                string query = "select * from " + NombreTabla + " where razon_social LIKE '" + nombre + "%'";
                dr = this.traerDatosConsulta(query);
            }
            catch (Exception e)
            {
                throw new SqlExecutionException("Error al verificar si existe el club", e);
            }

            if (dr.HasRows)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int getRows()
        {
            int rows;
            Conectar();
            string query = "Select MAX(id_club) FROM clubes";
            SqlCommand cmd = new SqlCommand(query, Conexion);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            Conexion.Close();
            rows = Int32.Parse(dt.Rows[0].ItemArray[0].ToString());
            return rows;
        }

        public bool ExistePorNombre(string nombre)
        {
            bool existe = false;
            string query = "SELECT * FROM clubes WHERE razon_social LIKE @nombre";
            string comando = string.Format("Select * FROM clubes where razon_social='{0}'", nombre);
            SqlDataReader dr = traerDatosConsulta(comando);
            if (dr.HasRows)
            {
                existe = true;
            }
            return existe;
        }


        public SqlDataReader ObtenerNombre()
        {
            Conectar();
            SqlDataReader dr = null;
            try
            {
                Cmd.CommandText = "select cod_club, razon_social from clubes";
                return dr;
            }
            catch (Exception e)
            {
                throw new SqlExecutionException("Error al obtener clubes", e);
            }
        }
    }
}
